home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / trace / tcpdump-2.2.1 / print-ntp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-03  |  6.3 KB  |  278 lines

  1. /*
  2.  * Copyright (c) 1988-1990 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that: (1) source code distributions
  7.  * retain the above copyright notice and this paragraph in its entirety, (2)
  8.  * distributions including binary code include the above copyright notice and
  9.  * this paragraph in its entirety in the documentation or other materials
  10.  * provided with the distribution, and (3) all advertising materials mentioning
  11.  * features or use of this software display the following acknowledgement:
  12.  * ``This product includes software developed by the University of California,
  13.  * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14.  * the University nor the names of its contributors may be used to endorse
  15.  * or promote products derived from this software without specific prior
  16.  * written permission.
  17.  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18.  * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20.  *
  21.  * Format and print ntp packets.
  22.  *    By Jeffrey Mogul/DECWRL
  23.  *    loosely based on print-bootp.c
  24.  */
  25.  
  26. #ifndef lint
  27. static char rcsid[] =
  28.     "@(#) $Header: print-ntp.c,v 1.7 92/01/04 01:45:16 leres Exp $ (LBL)";
  29. #endif
  30.  
  31. #include <stdio.h>
  32.  
  33. #include <sys/param.h>
  34. #include <sys/types.h>
  35. #include <sys/socket.h>
  36. #include <net/if.h>
  37. #include <netinet/in.h>
  38. #include <netinet/if_ether.h>
  39. #include <strings.h>
  40. #include <ctype.h>
  41.  
  42. #include "interface.h"
  43. #include "addrtoname.h"
  44. #include "ntp.h"
  45.  
  46. /*
  47.  * Print ntp requests
  48.  */
  49. void
  50. ntp_print(bp, length)
  51.     register struct ntpdata *bp;
  52.     int length;
  53. {
  54.     u_char *ep;
  55.     int mode, version, leapind;
  56.     static char rclock[5];
  57.  
  58. #define TCHECK(var, l) if ((u_char *)&(var) > ep - l) goto trunc
  59.  
  60.     /* Note funny sized packets */
  61.     if (length != sizeof(struct ntpdata))
  62.         (void)printf(" [len=%d]", length);
  63.  
  64.     /* 'ep' points to the end of avaible data. */
  65.     ep = (u_char *)snapend;
  66.  
  67.     TCHECK(bp->status, sizeof(bp->status));
  68.  
  69.     version = (bp->status & VERSIONMASK) >> 3;
  70.     printf(" v%d", version);
  71.  
  72.     leapind = bp->status & LEAPMASK;
  73.     switch (leapind) {
  74.  
  75.     case NO_WARNING:
  76.         break;
  77.  
  78.     case PLUS_SEC:
  79.         fputs(" +1s", stdout);
  80.         break;
  81.  
  82.     case MINUS_SEC:
  83.         fputs(" -1s", stdout);
  84.         break;
  85.     }
  86.  
  87.     mode = bp->status & MODEMASK;
  88.     switch (mode) {
  89.  
  90.     case MODE_UNSPEC:    /* unspecified */
  91.         fputs(" unspec", stdout);
  92.         break;
  93.  
  94.     case MODE_SYM_ACT:    /* symmetric active */
  95.         fputs(" sym_act", stdout);
  96.         break;
  97.  
  98.     case MODE_SYM_PAS:    /* symmetric passive */
  99.         fputs(" sym_pas", stdout);
  100.         break;
  101.  
  102.     case MODE_CLIENT:    /* client */
  103.         fputs(" client", stdout);
  104.         break;
  105.  
  106.     case MODE_SERVER:    /* server */
  107.         fputs(" server", stdout);
  108.         break;
  109.  
  110.     case MODE_BROADCAST:    /* broadcast */
  111.         fputs(" bcast", stdout);
  112.         break;
  113.  
  114.     case MODE_RES1:        /* reserved */
  115.         fputs(" res1", stdout);
  116.         break;
  117.  
  118.     case MODE_RES2:        /* reserved */
  119.         fputs(" res2", stdout);
  120.         break;
  121.  
  122.     }
  123.  
  124.     TCHECK(bp->stratum, sizeof(bp->stratum));
  125.     printf(" strat %d", bp->stratum);
  126.  
  127.     TCHECK(bp->ppoll, sizeof(bp->ppoll));
  128.     printf(" poll %d", bp->ppoll);
  129.  
  130.     /* Can't TCHECK bp->precision bitfield so bp->distance + 0 instead */
  131.     TCHECK(bp->distance, 0);
  132.     printf(" prec %d", bp->precision);
  133.  
  134.     if (!vflag)
  135.         return;
  136.  
  137.     TCHECK(bp->distance, sizeof(bp->distance));
  138.     fputs(" dist ", stdout);
  139.     p_sfix(&bp->distance);
  140.  
  141.     TCHECK(bp->dispersion, sizeof(bp->dispersion));
  142.     fputs(" disp ", stdout);
  143.     p_sfix(&bp->dispersion);
  144.  
  145.     TCHECK(bp->refid, sizeof(bp->refid));
  146.     fputs(" ref ", stdout);
  147.     /* Interpretation depends on stratum */
  148.     switch (bp->stratum) {
  149.  
  150.     case UNSPECIFIED:
  151.     case PRIM_REF:
  152.         strncpy(rclock, (char *)&(bp->refid), 4);
  153.         rclock[4] = '\0';
  154.         fputs(rclock, stdout);
  155.         break;
  156.  
  157.     case INFO_QUERY:
  158.         printf("%s INFO_QUERY", ipaddr_string(&(bp->refid)));
  159.         /* this doesn't have more content */
  160.         return;
  161.  
  162.     case INFO_REPLY:
  163.         printf("%s INFO_REPLY", ipaddr_string(&(bp->refid)));
  164.         /* this is too complex to be worth printing */
  165.         return;
  166.  
  167.     default:
  168.         printf("%s", ipaddr_string(&(bp->refid)));
  169.         break;
  170.     }
  171.  
  172.     TCHECK(bp->reftime, sizeof(bp->reftime));
  173.     putchar('@');
  174.     p_ntp_time(&(bp->reftime));
  175.  
  176.     TCHECK(bp->org, sizeof(bp->org));
  177.     fputs(" orig ", stdout);
  178.     p_ntp_time(&(bp->org));
  179.  
  180.     TCHECK(bp->rec, sizeof(bp->rec));
  181.     fputs(" rec ", stdout);
  182.     p_ntp_delta(&(bp->org), &(bp->rec));
  183.  
  184.     TCHECK(bp->xmt, sizeof(bp->xmt));
  185.     fputs(" xmt ", stdout);
  186.     p_ntp_delta(&(bp->org), &(bp->xmt));
  187.  
  188.     return;
  189.  
  190. trunc:
  191.     fputs(" [|ntp]", stdout);
  192. #undef TCHECK
  193. }
  194.  
  195. p_sfix(sfp)
  196.     register struct s_fixedpt *sfp;
  197. {
  198.     register int i;
  199.     register int f;
  200.     register float ff;
  201.  
  202.     i = ntohs(sfp->int_part);
  203.     f = ntohs(sfp->fraction);
  204.     ff = f / 65536.0;    /* shift radix point by 16 bits */
  205.     f = ff * 1000000.0;    /* Treat fraction as parts per million */
  206.     printf("%d.%06d", i, f);
  207. }
  208.  
  209. #define    FMAXINT    (4294967296.0)    /* floating point rep. of MAXINT */
  210.  
  211. p_ntp_time(lfp)
  212.     register struct l_fixedpt *lfp;
  213. {
  214.     register long i;
  215.     register unsigned long uf;
  216.     register unsigned long f;
  217.     register float ff;
  218.  
  219.     i = ntohl(lfp->int_part);
  220.     uf = ntohl(lfp->fraction);
  221.     ff = uf;
  222.     if (ff < 0.0)        /* some compilers are buggy */
  223.         ff += FMAXINT;
  224.     ff = ff / FMAXINT;    /* shift radix point by 32 bits */
  225.     f = ff * 1000000000.0;    /* treat fraction as parts per billion */
  226.     printf("%lu.%09d", i, f);
  227. }
  228.  
  229. /* Prints time difference between *lfp and *olfp */
  230. p_ntp_delta(olfp, lfp)
  231.     register struct l_fixedpt *olfp;
  232.     register struct l_fixedpt *lfp;
  233. {
  234.     register long i;
  235.     register unsigned long uf;
  236.     register unsigned long ouf;
  237.     register unsigned long f;
  238.     register float ff;
  239.     int signbit;
  240.  
  241.     i = ntohl(lfp->int_part) - ntohl(olfp->int_part);
  242.  
  243.     uf = ntohl(lfp->fraction);
  244.     ouf = ntohl(olfp->fraction);
  245.  
  246.     if (i > 0) {        /* new is definitely greater than old */
  247.         signbit = 0;
  248.         f = uf - ouf;
  249.         if (ouf > uf)    /* must borrow from high-order bits */
  250.             i -= 1;
  251.     } else if (i < 0) {    /* new is definitely less than old */
  252.         signbit = 1;
  253.         f = ouf - uf;
  254.         if (uf > ouf)    /* must carry into the high-order bits */
  255.             i += 1;
  256.         i = -i;
  257.     } else {        /* int_part is zero */
  258.         if (uf > ouf) {
  259.             signbit = 0;
  260.             f = uf - ouf;
  261.         } else {
  262.             signbit = 1;
  263.             f = ouf - uf;
  264.         }
  265.     }
  266.  
  267.     ff = f;
  268.     if (ff < 0.0)        /* some compilers are buggy */
  269.         ff += FMAXINT;
  270.     ff = ff / FMAXINT;    /* shift radix point by 32 bits */
  271.     f = ff * 1000000000.0;    /* treat fraction as parts per billion */
  272.     if (signbit)
  273.         putchar('-');
  274.     else
  275.         putchar('+');
  276.     printf("%d.%09d", i, f);
  277. }
  278.